﻿/**************************************************************
 * St. Sana - A file/folder scanner originall designed for    *
 * Open Media Library (open-media-library.googlecode.com)     *
 *                                                            *
 * Available for use by ANY project that wants to use it.     *
 * St. Sana is covered by the Apache 2 license.               *
 * Feel free to use this software however you like so as long *
 * as you follow the guidelines of above mentioned license.   *
 * Written by David J. Shultz (djshultz@gmail.com)            *
 **************************************************************/

using System;
using System.Collections.Generic;

namespace StSana
{
    public class Entity
    {
        /// <summary>
        /// private IList if Series objects that reside in this Entity
        /// </summary>
        IList<Series> seriesList;
        string name;

        public String Name
        {
            get { return name; }
            set
            {
                name = value;
            }
        }

        public Serf.EntityType EntityType
        {
            get; set;
        }
        /// <summary>
        /// Public IList access to all the Series objects in this Entity
        /// </summary>
        public IList<Series> Series
        {
            get { return seriesList; }
        }

        /// <summary>
        /// Empty constructor
        /// </summary>
        public Entity()
        {
            seriesList = new List<Series>();
        }

        static public Entity FindOrCreateForName(IList<Entity> entities, string name)
        {
            foreach (Entity entity in entities)
            {
                if (entity.Name.ToLowerInvariant().CompareTo(name.ToLowerInvariant()) == 0)
                    return entity;
            }

            Entity ent = new Entity();
            ent.name = name;
            return ent;
        }
        /// <summary>
        /// Adds a Series object to this Entity
        /// </summary>
        /// <param name="series">A Series object to be added</param>
        public void Add(Series series)
        {
            int loc = ContainsSeries(series.Name);
            if (loc > -1)
                seriesList.RemoveAt(loc);

            seriesList.Add(series);
        }

        public int ContainsSeries(string seriesName)
        {
            foreach (Series ser in seriesList)
            {
                if (!string.IsNullOrEmpty(ser.Name) && ser.Name.ToLower().CompareTo(seriesName.ToLower()) == 0)
                    return seriesList.IndexOf(ser);
            }
            return -1;
        }

        /// <summary>
        /// Removes a Series object from this Entity (if it exists)
        /// </summary>
        /// <param name="series">A Series object to be removed (if it exists)</param>
        /// <returns>The Series object that was removed.</returns>
        public Series Remove(Series series)
        {
            if (seriesList.Contains(series))
            {
                seriesList.Remove(series);
                return series;
            }
            return null;
        }
    }
}
